package com.liuyi.netty.im.test.im.listeners;

import com.alibaba.fastjson.JSONObject;
import com.liuyi.netty.im.commons.codes.data.event.Message;
import com.liuyi.netty.im.commons.codes.data.event.message.playload.Playload;
import com.liuyi.netty.im.test.modular.Kpi;
import com.liuyi.netty.im.test.util.ImHelp;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * 接受消息
 *
 * @author Mr.Fmy
 * @since 2022/1/19
 */
@Slf4j
public class ImMessageReceivedListener extends ImAbstractListener<Message<? extends Playload>> {

    @Override
    public void handler0(Channel channel, Message<? extends Playload> data) {
        Kpi.msgReceiveNum();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message<? extends Playload> convert(Object data) {
        if (data == null) {
            return null;
        }
        if (data instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) data;
            Message<? super Playload> message = jsonObject.toJavaObject(Message.class);
            message.setPlayload(ImHelp.decodePlayload(jsonObject.getJSONObject("playload"), message.getMsgType().getTClass()));
            return message;
        }
        return null;
    }
}
